var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;


    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });

    function xmSelectData(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            toolbar:{
                show:true,
                list:['ALL', 'CLEAR']
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }

    let warehouseList = [];
    let personNoList=[];
    let operationCodeList=[];
    layer.ready(function () {
        //获取仓库信息
        $.ajax({
            url:"transfer/getWarehouse",
            success:function(data){
                if(data.status == 200){
                    let warehouse = [];
                    for(let i in data.data){
                        let tmp = {
                            name:data.data[i].name,
                            value:data.data[i].code
                        }
                        warehouse.push(tmp);
                    }
                    warehouseList = xmSelectData("#warehouseCode", "warehouseCode", warehouse);
                    form.render('select');
                }
            }
        });

        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if(treeList && treeList.list){
            let userTree = getAllJson(treeList.list);
            personNoList=getxmselect("#personNo","personNo", userTree);
            operationCodeList=getxmselect("#operationCode","operationCode", userTree);
        }else {
            personNoList=getxmselect("#personNo","personNo", [{name: user.name, value: user.code}]);
            operationCodeList=getxmselect("#operationCode","operationCode", [{name: user.name, value: user.code}]);
        }


    });

//表单刷新
    form.render();

    let initData = [];
    table.render({
        elem: '#parentTable'
        ,data:initData
        ,toolbar: '#parentTableToolbar'
        ,totalRow: true
        ,cols: [[
            {field:'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'},
            {field:'billNo', title:'入库单号',sort:true, align: 'center', width: 180},
            {field:'createDate', title:'入库日期',sort:true, align: 'center', width: 120},
            {field:'person', title:'入库人',sort:true, align: 'center', width: 120},
            {field:'warehouseName', title:'仓库名称',sort:true, align: 'center', width: 190},
            {field:'productCode', title:'产品编码',sort:true, align: 'center', width: 120},
            {field:'specificationCode', title:'规格编码',sort:true, align: 'center', width: 190},
            {field:'specificationExplanation', title:'规格说明',sort:true, align: 'center', width: 120},
            {field:'clientProductNo', title:'客户货号',sort:true, align: 'center', width: 120},
            {field:'purchaseNo', title:'采购单号',sort:true, align: 'center', width: 120},
            {field:'supplierName', title:'供应商名称',sort:true, align: 'center', width: 120},
            {field:'productUnit', width: 120, title: '产品单位', sort: true, align: 'center'},
            {field:'productNum', width: 120, title: '产品数量', sort: true, align: 'center', totalRow: true},
            {field:'trayNumber', width: 120, title: '托盘入数', sort: true, align: 'center', totalRow: true},
            {field:'totalTray', width: 120, title: '总托盘', sort: true, align: 'center', totalRow: true},
            {field:'productCname', width: 120, title: '中文名称', sort: true, align: 'center'},
            {field:'productEname', width: 120, title: '英文名称', sort: true, align: 'center'},
            {field:'productCdesc', width: 120, title: '中文描述', sort: true, align: 'center'},
            {field:'productEdesc', width: 120, title: '英文描述', sort: true, align: 'center'},
            {field:'memo', width: 150, title: '备注', sort: true, align: 'center'}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#parentTable').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                curr: 1
            },
            url:'otherInStock/statistics/productInfo',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"otherInStock/export/productInfoExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '其它入库产品统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

    document.getElementById('clear').onclick = function(){
        warehouseList.setValue([ ]);
        personNoList.setValue([ ]);
        operationCodeList.setValue([ ]);
    };


    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["code"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }
    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }



});

